#include "gnurilla.h"

void gnurilla_notebook_array_append (GnurillaData *gd, int tab_number,
				     char *status, char *filename, char *path)
{
	gd->notebook_array[tab_number][0] = status;
	gd->notebook_array[tab_number][1] = filename;
	gd->notebook_array[tab_number][2] = path;
};

void gnurilla_notebook_array_remove (GnurillaData *gd, int tab_number)
{
	gd->notebook_array[tab_number][0] = NULL;
	gd->notebook_array[tab_number][1] = NULL;
	gd->notebook_array[tab_number][2] = NULL;
};

void gnurilla_notebook_array_modify_status (GnurillaData *gd, int tab_number, char *status)
{
	gd->notebook_array[tab_number][0] = status;
};

void gnurilla_add_notebook_page (GtkAction *action, GnurillaData *gd)
{
	GtkWidget *page_title;
	GtkWidget *scrolled_window;
	GtkWidget *text_view;
	GtkTextBuffer *text_buffer;
	
	text_buffer = gtk_text_buffer_new(NULL);
	text_view = gtk_text_view_new_with_buffer(text_buffer);
	page_title = gnurilla_create_notebook_label ("Nuovo");
	
	scrolled_window = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_placement(GTK_SCROLLED_WINDOW(scrolled_window), GTK_CORNER_TOP_LEFT);
	gtk_container_add(GTK_CONTAINER(scrolled_window), text_view);
	
	gtk_notebook_append_page(GTK_NOTEBOOK(gd->notebook), scrolled_window, page_title);
	gtk_widget_show_all(GTK_WIDGET(gd->notebook));
	gtk_notebook_set_current_page(GTK_NOTEBOOK(gd->notebook), -1);
	gtk_widget_grab_focus(GTK_WIDGET(text_view));
};

void gnurilla_remove_notebook_page ()
{
	/* printf("HA! Notebook page closed!\n"); */
};

GtkWidget *gnurilla_create_notebook_close_button ()
{
	GtkWidget *close_button;
	GtkWidget *close_button_image;
	
	close_button_image = gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
	close_button = gtk_button_new();
	g_signal_connect(G_OBJECT(close_button), "clicked",
			 G_CALLBACK(gnurilla_remove_notebook_page), NULL);
	gtk_container_add(GTK_CONTAINER(close_button), GTK_WIDGET(close_button_image));
	gtk_container_set_border_width(GTK_CONTAINER(close_button), 0);
	gtk_button_set_relief(GTK_BUTTON(close_button), GTK_RELIEF_NONE);
	gtk_widget_set_size_request(GTK_WIDGET(close_button), 18, 19);
	
	return close_button;
};

GtkWidget *gnurilla_create_notebook_label (char *title)
{
	GtkWidget *hbox;
	GtkWidget *title_label;
	GtkWidget *close_button;
	
	title_label = gtk_label_new(title);
	close_button = gnurilla_create_notebook_close_button();
	
	hbox = gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), title_label, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), close_button, FALSE, FALSE, 1);
	gtk_widget_show_all(GTK_WIDGET(hbox));
	
	return hbox;
};

GtkWidget *gnurilla_create_notebook ()
{
	GtkWidget *notebook;
	
	notebook = gtk_notebook_new();
	g_signal_connect (G_OBJECT(notebook), "switch-page",
			  G_CALLBACK(gnurilla_cb_notebook_switch_page), NULL);
	gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
	
	return notebook;
}
